package views;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import GsonBean.AddPointBean;
import GsonBean.RjhSgxmListBean;
import app.MyApplication;

import dialog.EditDangerPointDailog;

import greendao.SecurityRiskDao;

import greendao.entity.ProtectMeasures;


/**
 * Created by zx on 2017/3/27.
 * 自定义风险点View
 */

public class AddDangerPointView extends LinearLayout {


    //安全风险内容 数据源
    ArrayList<String> aqfxnrList;
    //安全风险措施 数据源
    ArrayList<String> aqfxcsList;

    TextView aqfxnr, aqfxcs;

    //删除风险点
    TextView scfxd;

    int position = 0;
    EditDangerPointDailog mEditDangerPointDailog, mEditDangerPointDailog2;

    //给选择风险点dialog提供数据， 内容为该项目： 风险或措施名称 为 Key，对应编号为string方便后续处理；
    HashMap<String, Integer> mMapData;

    SecurityRiskDao mSecurityRiskDao;

    public AddDangerPointView(Context context, HashMap<String, Integer> mMap) {
        super(context);
        this.mMapData = mMap;
        setView();
        InitData();
        InitListen();
    }

    public AddDangerPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setView();
        InitData();
        InitListen();
    }

    public AddDangerPointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView();
        InitData();
        InitListen();
    }

    public void setView() {
        View view = View.inflate(getContext(), R.layout.bcfxd_edit, null);
        this.addView(view);
        //新增宽高设置，让宽度填充整个父布局
        LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = LinearLayout.LayoutParams.MATCH_PARENT;
        params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        view.setLayoutParams(params);
        aqfxnr = (TextView) view.findViewById(R.id.aqfxnr);
        aqfxcs = (TextView) view.findViewById(R.id.aqfxcs);
        scfxd = (TextView) view.findViewById(R.id.scfxd);
        //设置margintop 界面美观
        LinearLayout.LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.topMargin = 5;
        setLayoutParams(lp);
    }

    public void InitData() {
        scfxd.setVisibility(VISIBLE);
        mSecurityRiskDao = MyApplication.getDaoSession().getSecurityRiskDao();
        aqfxnrList = new ArrayList<>();
        aqfxcsList = new ArrayList<>();
    }

    public void InitListen() {
        //删除风险点
        scfxd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewGroup) AddDangerPointView.this.getParent()).removeView(AddDangerPointView.this);
            }
        });
        //安全风险内容
        aqfxnr.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditDangerPointDailog(0, aqfxnr.getId());
            }
        });
        //安全风险措施
        aqfxcs.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showEditDangerPointDailog2(1, aqfxcs.getId());
            }
        });
    }

    public void showEditDangerPointDailog(int flag, int ViewId) {
        if (mEditDangerPointDailog == null) {
            mEditDangerPointDailog = new EditDangerPointDailog(getContext(), mMapData, flag, ViewId);
            mEditDangerPointDailog.setOnOkClickListener(new EditDangerPointDailog.OnOkClickListener() {
                @Override
                public void OkClickListener(String s, int ViewId) {
                    TextView tv = (TextView) findViewById(ViewId);
                    tv.setText(s);
                    mEditDangerPointDailog.dismiss();
                }
            });
        }
        mEditDangerPointDailog.show();
    }

    public void showEditDangerPointDailog2(int flag, int ViewId) {
        if (mEditDangerPointDailog2 == null) {
            mEditDangerPointDailog2 = new EditDangerPointDailog(getContext(), mMapData, flag, ViewId);
            mEditDangerPointDailog2.setOnOkClickListener(new EditDangerPointDailog.OnOkClickListener() {
                @Override
                public void OkClickListener(String s, int ViewId) {
                    TextView tv = (TextView) findViewById(ViewId);
                    tv.setText(s);
                    mEditDangerPointDailog2.dismiss();
                }
            });
        }
        mEditDangerPointDailog2.show();
    }

    public void setAqfxData(AddPointBean.SupplementFxdListBean bean) {
        aqfxnr.setText(bean.getAqfxnr());
        aqfxcs.setText(bean.getAqfxcsnr());
    }

    public void readOnly(){
        aqfxnr.setOnClickListener(null);
        aqfxcs.setOnClickListener(null);
        scfxd.setVisibility(GONE);
    }

}
